package com.topisv.tms.repository.lpn;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.lpn.StorageOrder;

public interface StorageOrderDao
    extends PagingAndSortingRepository<StorageOrder, Long>, JpaSpecificationExecutor<StorageOrder>
{
    
    @Modifying
    @Query("UPDATE StorageOrder SET status = ?1, updateTime = NOW() WHERE lpnNo in ?2 ")
    int modifyStatusByLpnNos(int status, List<String> lpnNos);
    
    @Modifying
    @Query("UPDATE StorageOrder SET status = ?1, updateTime = NOW() WHERE  lpnNo in ?2 and batchSeq = ?3")
    int modifyStatusByLpnNoAndBatch(int status, String lpnNo,int batchSeq);
    
    @Modifying
    @Query("UPDATE StorageOrder SET status = ?1, updateTime = NOW() WHERE lpnNo = ?2 ")
    int modifyStatusByLpnNos(int status, String lpnNo);
    
    @Modifying
    @Query("UPDATE StorageOrder SET status = ?1, updateTime = NOW() WHERE orderNo in ?2 ")
    int modifyStatusByOrderNos(int status, List<String> orderNos);
    
    @Modifying
    @Query("UPDATE StorageOrder SET status = ?1, updateTime = NOW() WHERE orderNo = ?2 ")
    int modifyStatusByOrderNos(int status, String orderNo);
    
    @Query("SELECT MAX(batchSeq) FROM StorageOrder WHERE lpnNo = ?1 ")
    Integer getMaxBatchSeqByLpn(String lpn);
    
    @Modifying
    @Query("UPDATE StorageOrder SET status = ?1, updateTime = NOW() WHERE status = ?2 and  lpnNo in ?3 ")
    int modifyStatusByLpnNos(int status, int oldStatus, List<String> lpnNos);
    
    
    
    @Modifying
    @Query("UPDATE StorageOrder SET status = ?1, updateTime = NOW() WHERE status = ?2 and  lpnNo = ?3 ")
    int modifyStatusByLpnNos(int status, int oldStatus, String lpnNo);
    
    @Query("SELECT s FROM StorageOrder s WHERE orderNo = ?1 and status = ?2 ")
    StorageOrder getStorageOrderByOrderAndStatus(String orderNo, int status);
    
    @Query("SELECT s FROM StorageOrder s WHERE orderNo in ?1 and status = ?2 ")
    List<StorageOrder> getStorageOrderByOrderAndStatus(List<String> orderNo, int status);
    
    @Query("SELECT s FROM StorageOrder s WHERE lpnNo = ?1 and status = ?2 ")
    List<StorageOrder> getStorageOrderByLpn(String lpn,int status);
    
    @Query("SELECT s FROM StorageOrder s WHERE lpnNo = ?1")
    List<StorageOrder> getStorageOrderByLpn(String lpn);
    
    @Query("SELECT s FROM StorageOrder s WHERE lpnNo = ?1 and orderNo = ?2 ")
    List<StorageOrder> getStorageOrderByLpnAndOrderNo(String lpn,String OrderNo);
    
}
